/**
 * @Copyright:Copyright (c) 1992 - 2015
 * @Company: TengTong
 */
package com.tengtong.duang.commons.convert.support;

import java.util.Arrays;

import com.tengtong.duang.commons.collections.ArrayUtils;
import com.tengtong.duang.commons.convert.ConditionalConverter;
import com.tengtong.duang.commons.convert.ConversionService;

/**
 *   Array 转成字符串
 *
 * @author jai
 * @since: v1.0
 */
public class ArrayToStringConverter implements ConditionalConverter{

	private final CollectionToStringConverter helperConverter;
	
	public ArrayToStringConverter(ConversionService helperConverter){
		this.helperConverter = new CollectionToStringConverter( helperConverter );
	}
	
	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.convert.ConditionalConverter#convert(java.lang.Object, java.lang.Class)
	 */
	@Override
	public Object convert(Object source, Class targetType) {
		return this.helperConverter.convert(Arrays.asList(ArrayUtils.toObjectArray(source)), targetType);
	}

	/* (non-Javadoc)
	 * @see com.tengtong.duang.commons.convert.ConditionalConverter#matches(java.lang.Class, java.lang.Class)
	 */
	@Override
	public boolean matches(Class<?> sourceType, Class<?> targetType) {
		return sourceType.isArray() && String.class.equals(targetType);
	}

}
